package com.cow.support.remote;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cow.framework.core.base.BaseComponent;
import com.cow.support.dto.OrgInfoDTO;
import com.cow.support.dto.User;
import com.cow.support.entity.Orginfo;
import com.cow.support.entity.Resourceinfo;
import com.cow.support.entity.Roleinfo;
import com.cow.support.entity.Userinfo;
import com.cow.support.entity.Visitlog;
import com.cow.support.service.LogService;
import com.cow.support.service.OrgService;
import com.cow.support.service.ResourceService;
import com.cow.support.service.RoleService;
import com.cow.support.service.UserService;
import com.cow.support.sys.exception.BusinessException;
import com.cow.support.util.DateUtil;

@Service
public class AuthRemoteService extends BaseComponent implements IAuthRemoteService{
	@Autowired
	private UserService userService;
	@Autowired
	private RoleService roleService;
	@Autowired
	private ResourceService resourceService;
	@Autowired
	private LogService logService;
	@Autowired
	private OrgService orgService;
	

	public String registClient(String client){
		try {
			RemoteClientManage.registClient(client);
			return "1";
		} catch (Exception e) {
			e.printStackTrace();
			return "0";
		}		
	}


	public User loadUserByAccountname(String accountName,String systemID) {
		try {

			Userinfo userinfo = userService.getUserByAccountName(accountName);
			if (userinfo == null) {
				throw new BusinessException("用户不存在！");
			}
			List<Roleinfo> roleList = roleService.getUserRole(userinfo.getUserid());
			User user = new User();
			PropertyUtils.copyProperties(user, userinfo);
			user.setOrgInfoDTO(getOrgById(userinfo.getOrgid()));

			if( user.getJob() != null){
				user.setJobName(dictUtil.getDictValueName("job", user.getJob().toString()));			
			}			
			user.setRoleList(roleList);
			
			HashMap<String,List<Resourceinfo>> resourceMap = resourceService.getUserResource(userinfo.getUserid(), systemID);
			user.setResourceMap(resourceMap);
			
			
			if(!(systemID.equals("0")||systemID.equals("1")||systemID.equals("10")||systemID.equals("14")||systemID.equals("19"))){
				Visitlog visitLog = new Visitlog();
				visitLog.setLogintime(DateUtil.getCurrentDate());
				visitLog.setUsername(accountName);			
				visitLog.setSystemid(systemID);
				visitLog.setVisittype("1");
				visitLog.setIssuccess("1");
				logService.saveVisitLog(visitLog);
			}						
			return user;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}
	
	public String saveLoginLog(Visitlog log){
		try{
			logService.saveVisitLog(log);
			return "1";
		}catch (Exception e) {
			e.printStackTrace();
			return "0";
		}
	}

	public User loadUserByAccountName(String accountName) {
		try {
			Userinfo userinfo = userService.getUserByAccountName(accountName);
			if (userinfo == null) {
				throw new BusinessException("用户不存在！");
			}
			List<Roleinfo> roleList = roleService.getUserRole(userinfo.getUserid());
			User user = new User();
			PropertyUtils.copyProperties(user, userinfo);
			user.setOrgInfoDTO(getOrgById(userinfo.getOrgid()));

			if( user.getJob() != null){
				user.setJobName(dictUtil.getDictValueName("job", user.getJob().toString()));			
			}				user.setRoleList(roleList);
					
			return user;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	public User getUserById(Integer userID){
		try {
			Userinfo userinfo = userService.getUserByID(userID);
			if (userinfo == null) {
				throw new BusinessException("用户不存在！");
			}
			List<Roleinfo> roleList = roleService.getUserRole(userinfo.getUserid());
			User user = new User();
			PropertyUtils.copyProperties(user, userinfo);
			user.setOrgInfoDTO(getOrgById(userinfo.getOrgid()));
			if( user.getJob() != null){
				user.setJobName(dictUtil.getDictValueName("job", user.getJob().toString()));			
			}				user.setRoleList(roleList);		
			return user;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public String getUserMobileTel(String accountName){
		try {
			Userinfo userinfo = userService.getUserByAccountName(accountName);
			if (userinfo == null) {
				throw new BusinessException("用户不存在！");
			}
			return userinfo.getMobile();
		} catch (Exception e) {
			e.printStackTrace();
			return "0";
		} 
	}
	

	public Integer register(User user) {		
		try {
			Userinfo userinfo = new Userinfo();
			PropertyUtils.copyProperties(userinfo, user);
			return userService.saveUser(userinfo);
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}

	public String modifyUserInfo(User user){
		try {
			Userinfo userinfo = userService.getUserByID(user.getUserid());
			if (userinfo == null) {
				throw new BusinessException("用户不存在！");
			}			
			userinfo.setAuthlevel(user.getAuthlevel());
			userinfo.setEmail(user.getEmail());
			userinfo.setLockflag(user.getLockflag());
			userinfo.setLockreason(user.getLockreason());
			userinfo.setMobile(user.getMobile());
			userinfo.setTelephone(user.getTelephone());
			userinfo.setUsername(user.getUsername());
			userService.updateUser(userinfo);
			return "1";
		} catch (Exception e) {
			e.printStackTrace();
			return "0";
		} 
	}
	

	public String modifyPassword(Integer userid,String oldPassword,String newPassword){
		try {
			Userinfo userinfo = userService.getUserByID(userid);
			if (userinfo == null) {
				return "01";
			}			

			if(!oldPassword.equals(userinfo.getPassword())){
				return "02";
			}
			userinfo.setPassword(newPassword);
			
			userService.updateUser(userinfo);
			return "1";
		} catch (Exception e) {
			e.printStackTrace();
			return "0";
		} 
	}
	
	public List<OrgInfoDTO> getOrgs(){
		List<Orginfo> orgList = orgService.getAllOgr();
		List<OrgInfoDTO> orgdtoList = new ArrayList<OrgInfoDTO>(); 
		for(Orginfo orginfo:orgList){
			OrgInfoDTO orgDTO = new OrgInfoDTO();

			try {
				PropertyUtils.copyProperties(orgDTO, orginfo);
				orgdtoList.add(orgDTO);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return orgdtoList;
	}
	
	public List<User> getOrgUser(Integer orgId){
		try {
			List<Userinfo> userList = userService.getUserByOrgID(orgId);
			List<User> userResultList = new ArrayList<User>();
			for(Userinfo dto:userList){
				List<Roleinfo> roleList = roleService.getUserRole(dto.getUserid());
				User user = new User();
				PropertyUtils.copyProperties(user, dto);
				user.setOrgInfoDTO(getOrgById(dto.getOrgid()));
				if( user.getJob() != null){
					user.setJobName(dictUtil.getDictValueName("job", user.getJob().toString()));			
				}					user.setRoleList(roleList);		
				userResultList.add(user);
			}
			return userResultList;
		}catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public OrgInfoDTO getOrgById(Integer orgId){
		try {
			Orginfo orginfo = orgService.getOrgByID(orgId);
			OrgInfoDTO dto = new OrgInfoDTO();
			PropertyUtils.copyProperties(dto, orginfo);
			return dto;
		}catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public List<Resourceinfo> getResourceByType(String systemID,String resourceType){
		return resourceService.findBySystemidAndResourcetype(systemID, resourceType);
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public ResourceService getResourceService() {
		return resourceService;
	}

	public void setResourceService(ResourceService resourceService) {
		this.resourceService = resourceService;
	}

	public LogService getLogService() {
		return logService;
	}

	public void setLogService(LogService logService) {
		this.logService = logService;
	}

	public OrgService getOrgService() {
		return orgService;
	}

	public void setOrgService(OrgService orgService) {
		this.orgService = orgService;
	}
	
}
